package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityAppBinding;
import flc.ast.fragment.AppFragment;
import flc.ast.fragment.HomeFragment;
import flc.ast.fragment.TabFragment;
import stark.common.basic.event.EventStatProxy;

public class AppActivity extends BaseAc<ActivityAppBinding> {

    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitle = new String[]{};
    private Integer[] tabImg = new Integer[]{};

    @Override
    protected int onCreate() {
        return R.layout.activity_app;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivAppBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        tabTitle = new String[]{getString(R.string.app_type1), getString(R.string.app_type2)};
        tabImg = new Integer[]{R.drawable.axitongapp, R.drawable.ayixiazaiapp};
        setTabData();
    }

    private void setTabData() {
        for (int i = 0; i < tabTitle.length; i++) {
            fragments.add(new AppFragment().newInstance(i));
            mDataBinding.tlApp.addTab(mDataBinding.tlApp.newTab().setText(tabTitle[i]));
        }
        mDataBinding.vpApp.setAdapter(new AppActivity.ArticleAdapter(getSupportFragmentManager()));
        mDataBinding.tlApp.setupWithViewPager(mDataBinding.vpApp);
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tlApp.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tlApp.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tlApp.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvTab);
        ImageView iv = view.findViewById(R.id.ivTab);
        iv.setVisibility(View.VISIBLE);
        tv.setVisibility(View.GONE);
        //设置TabLayout点击事件
        mDataBinding.tlApp.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                ImageView iv = view.findViewById(R.id.ivTab);
                iv.setVisibility(View.VISIBLE);
                tv.setVisibility(View.GONE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                ImageView iv = view.findViewById(R.id.ivTab);
                iv.setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class ArticleAdapter extends FragmentStatePagerAdapter {
        public ArticleAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_tab, null);
        TextView text = view.findViewById(R.id.tvTab);
        ImageView img = view.findViewById(R.id.ivTab);
        text.setText(tabTitle[i]);
        img.setImageResource(tabImg[i]);
        img.setVisibility(View.GONE);
        return view;
    }

}